package com.sp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sp.model.dto.EmployeeRoleDTO;
import com.sp.model.entity.EmployeeRole;
import com.sp.model.entity.PageResult;
import com.sp.model.entity.Result;
import com.sp.model.vo.EmployeeRoleVO;
import com.sp.model.vo.MenuVO;
import com.sp.model.vo.PermissionVO;
import com.sp.service.EmployeeRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.sp.constant.EmployeeRoleMessageConstant.*;

/**
 * @author tong
 * @date 2024/3/25 14:21
 */
@Api(tags = "管理员角色权限管理api接口")
@RestController
@RequestMapping("/employeeRoles")
public class EmployeeRoleController {

    @Autowired
    private EmployeeRoleService employeeRoleService;

    @ApiOperation("添加管理员权限角色")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'employee:role:add')")
    @PostMapping
    public Result<Object> addEmployeeRole(
            @ApiParam(name = "employeeRoleDTO", value = "管理员权限角色信息") @RequestBody EmployeeRoleDTO employeeRoleDTO) {
        if (employeeRoleService.addEmployeeRole(employeeRoleDTO)) {
            return Result.success(ADD_EMPLOYEE_ROLE_SUCCESS);
        }
        return Result.error(ADD_EMPLOYEE_ROLE_FAIL);
    }

    @ApiOperation("修改管理员权限角色")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'employee:role:update')")
    @PutMapping
    public Result<Object> updateEmployeeRole(
            @ApiParam(name = "employeeRoleDTO", value = "管理员权限角色信息") @RequestBody EmployeeRoleDTO employeeRoleDTO) {
        if (employeeRoleService.updateEmployeeRole(employeeRoleDTO)) {
            return Result.success(UPDATE_EMPLOYEE_ROLE_SUCCESS);
        }
        return Result.error(UPDATE_EMPLOYEE_ROLE_FAIL);
    }

    @ApiOperation("删除管理员权限角色")
    @PreAuthorize("hasAnyAuthority('boss', 'root', 'employee:main', 'employee:role:delete')")
    @DeleteMapping("/{ids}")
    public Result<Object> deleteEmployeeRole(
            @ApiParam(name = "ids", value = "管理员权限角色id列表") @PathVariable("ids") List<Integer> ids) {
        if (employeeRoleService.deleteEmployeeRoles(ids)) {
            return Result.success(DELETE_EMPLOYEE_ROLE_SUCCESS);
        }
        return Result.error(DELETE_EMPLOYEE_ROLE_FAIL);
    }

    @ApiOperation("条件查询管理员权限角色")
    @GetMapping("/page")
    public Result<PageResult<EmployeeRoleVO>> page(
            @ApiParam(name = "currentPage", value = "页码") Integer currentPage,
            @ApiParam(name = "pageSize", value = "每页条数") Integer pageSize,
            @ApiParam(name = "employeeRoleDTO", value = "管理员权限角色信息") EmployeeRoleDTO employeeRoleDTO) {
        IPage<EmployeeRole> pages = employeeRoleService.page(currentPage, pageSize, employeeRoleDTO);
        if (pages != null) {
            List<EmployeeRole> employeeRoles = pages.getRecords();

            List<EmployeeRoleVO> employeeRoleVOS = new ArrayList<>();
            for (EmployeeRole employeeRole : employeeRoles) {
                EmployeeRoleVO employeeRoleVO = BeanUtil.copyProperties(employeeRole, EmployeeRoleVO.class);

                employeeRoleVO.setPermissions(BeanUtil.copyToList(employeeRole.getPermissions(), PermissionVO.class));

                employeeRoleVO.setMenus(BeanUtil.copyToList(employeeRole.getMenus(), MenuVO.class));

                employeeRoleVOS.add(employeeRoleVO);
            }

            return Result.success(SELECT_EMPLOYEE_ROLE_SUCCESS
                    , new PageResult<>(pages.getSize(), pages.getTotal(), pages.getCurrent(), pages.getPages(), employeeRoleVOS));
        }
        return Result.error(SELECT_EMPLOYEE_ROLE_FAIL);
    }

    @ApiOperation("查询所有管理员权限角色")
    @GetMapping("/selectAll")
    public Result<List<EmployeeRoleVO>> selectAll() {
        List<EmployeeRole> employeeRoles = employeeRoleService.selectAll();

        if (employeeRoles == null || employeeRoles.size() == 0) {
            return Result.success("权限角色数据查询完毕，但数据为空，请联系管理员处理");
        }

        List<EmployeeRoleVO> employeeRoleVOS = new ArrayList<>();
        for (EmployeeRole employeeRole : employeeRoles) {
            EmployeeRoleVO employeeRoleVO = BeanUtil.copyProperties(employeeRole, EmployeeRoleVO.class);

            employeeRoleVO.setPermissions(BeanUtil.copyToList(employeeRole.getPermissions(), PermissionVO.class));

            employeeRoleVO.setMenus(BeanUtil.copyToList(employeeRole.getMenus(), MenuVO.class));

            employeeRoleVOS.add(employeeRoleVO);
        }
        return Result.success(SELECT_EMPLOYEE_ROLE_SUCCESS, employeeRoleVOS);
    }

}
